<script lang="ts" setup>
import type { DiningTableTypeApi } from '#/api/spa/dining/tabletype';

import { useVbenModal } from '@vben/common-ui';
import {
  message,
  Tabs,
  Checkbox,
  Input,
  Textarea,
  Select,
  RadioGroup,
  CheckboxGroup,
  DatePicker,
} from 'ant-design-vue';

import { computed, ref } from 'vue';
import { $t } from '#/locales';
import { useVbenForm } from '#/adapter/form';
import {
  getDiningTableType,
  createDiningTableType,
  updateDiningTableType,
} from '#/api/spa/dining/tabletype';

import { useFormSchema } from '../data';

import { DICT_SPA_TYPE } from '#/utils/dictSpa';
const emit = defineEmits(['success']);
const formData = ref<DiningTableTypeApi.DiningTableType>();
const getTitle = computed(() => {
  return formData.value?.id
    ? $t('ui.actionTitle.edit', ['餐桌类型表（支持多餐厅）'])
    : $t('ui.actionTitle.create', ['餐桌类型表（支持多餐厅）']);
});

const [Form, formApi] = useVbenForm({
  commonConfig: {
    componentProps: {
      class: 'w-full',
    },
    formItemClass: 'col-span-2',
    labelWidth: 80,
  },
  layout: 'horizontal',
  schema: useFormSchema(),
  showDefaultActions: false,
});

const [Modal, modalApi] = useVbenModal({
  async onConfirm() {
    const { valid } = await formApi.validate();
    if (!valid) {
      return;
    }
    modalApi.lock();
    // 提交表单
    const data =
      (await formApi.getValues()) as DiningTableTypeApi.DiningTableType;
    try {
      await (formData.value?.id
        ? updateDiningTableType(data)
        : createDiningTableType(data));
      // 关闭并提示
      await modalApi.close();
      emit('success');
      message.success($t('ui.actionMessage.operationSuccess'));
    } finally {
      modalApi.unlock();
    }
  },
  async onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      formData.value = undefined;
      return;
    }
    // 加载数据
    let data = modalApi.getData<DiningTableTypeApi.DiningTableType>();
    if (!data) {
      return;
    }
    if (data.id) {
      modalApi.lock();
      try {
        data = await getDiningTableType(data.id);
      } finally {
        modalApi.unlock();
      }
    }
    // 设置到 values
    formData.value = data;
    await formApi.setValues(formData.value);
  },
});
</script>

<template>
  <Modal :title="getTitle">
    <Form class="mx-4" />
  </Modal>
</template>
